<?php
/**
 * Created by PhpStorm.
 * User: qhzhyt
 * Date: 2018/10/23
 * Time: 23:58
 */
require_once '../conf/conf.php';
if (isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
    if (!isset($_POST['password'])) {
        echo "<script language='javascript'>\n";
        echo "alert('请输入新密码!\\n');\n";
        echo "history.back();\n</script>";
        exit(1);
    }

    $password = $_POST['password'];

    session_start();

    if (!isset($_SESSION['verifyRecord'])) {
        echo "<script language='javascript'>\n";
        echo "alert('验证信息不存在或已失效!\\n');\n";
        echo "history.back();\n</script>";
        exit(1);
    }
    $record = $_SESSION['verifyRecord'];

    $db->update('user', ['password' => cryptPassword($password)], ['user_id' => $record['user_id']]);

    $user = $db->get('user', '*', ['user_id' => $record['user_id']]);

    if ($user == null || $user['password'] != cryptPassword($password)) {
        echo "<script language='javascript'>\n";
        echo "alert('重置密码失败，请重试！若多次失败，请尝试重新发送验证邮件。\\n');\n";
        echo "window.location.href='/';\n</script>";
        exit(1);
    }

    $db->delete('email_record',['verify_code' => $verifyCode]);

    echo "<script language='javascript'>\n";
    echo "alert('重置密码成功！\\n');\n";
    echo "window.location.href='/';\n</script>";
    exit(1);


} else {
    if (!isset($_GET['verifyCode']) || $_GET['verifyCode'] == null || strlen($_GET['verifyCode']) < 32) {
        echo "<script language='javascript'>\n";
        echo "alert('验证信息有误!\\n');\n";
        echo "window.location.href='/';\n</script>";
        exit(1);
    }
    $verifyCode = $_GET['verifyCode'];
    $record = $db->get('email_record', '*', ['verify_code' => $verifyCode]);
    if ($record == null || strtotime($record['send_time']) + 24 * 60 * 6 < time()) {
        echo "<script language='javascript'>\n";
        echo "alert('验证信息不存在或已失效!\\n');\n";
        echo "window.location.href='/';\n</script>";
        exit(1);
    }
    session_start();
    $_SESSION['verifyRecord'] = $record;
}


?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
    <title>重置登录密码-TouHouMaker</title>
</head>
<body>
<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    /*.bg-div{
        background: url('https://raw.githubusercontent.com/MrQingchun/lpy-nodejs/master/public/img/bg2.jpg') center no-repeat;
        width: 100%;
        height: 100%;
        text-align:center;
        position: absolute;
        top: auto;
        left: auto;
    }*/
    .signin {
        width: 100%;
        height: 100%;
        text-align: center;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
    }

    .user-avater {
        border-radius: 50%;
        border: 4px solid #fff;
    }

    /*边框 左下右*/
    .box {
        width: 400px;
        color: #666;
        text-align: center;
        margin-top: 30px;
        margin-bottom: 100px;
        border-radius: 4px;
        border: 2px solid rgba(255, 255, 255, 0.2);
        border-top: 0;
        position: relative;
    }

    /*边框 上三角*/
    .box::after {
        content: '';
        display: block;
        width: 40px;
        height: 40px;
        position: absolute;
        top: -20px;
        left: 50%;
        margin-left: -21px;
        border-top-left-radius: 4px;
        border-left: 2px solid rgba(255, 255, 255, 0.3);
        border-top: 2px solid rgba(255, 255, 255, 0.3);
        transform: rotate(45deg);
        box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.2);
    }

    .box::before {
        content: '';
        display: block;
        width: 400px;
        height: 2px;
        position: absolute;
        /* margin: -199px 199px; */
        border-top-left-radius: 4px;
        /* transform: rotate(90deg); */
        box-shadow: inset 171px 0 0 0 rgba(255, 255, 255, 0.2), inset -171px 0 0 0 rgba(255, 255, 255, 0.2);
    }

    .signin-form {
        height: calc(100% - 40px);
        padding: 20px;
    }

    .btn {
        color: #fff;
        display: inline-block;
        margin-bottom: 0;
        font-weight: normal;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        background-image: none;
        border: 1px solid transparent;
        white-space: nowrap;
        padding: 6px 12px;
        font-size: 13px;
        line-height: 1.42857143;
        border-radius: 0px;
        -webkit-user-select: none;
    }

    .btn-submit {
        width: 360px;
        height: 40px;
        background-color: #3B89F5;
        border-color: #3B89F5;
        margin-top: 20px;
    }

    .btn-submit:hover {
        color: #fff;
        background-color: #0C6CED;
        border-color: #0C6CED;
        text-decoration: none;
    }

    input::-webkit-input-placeholder {
        color: #242424;
    }

    .form-control {
        display: block;
        width: 100%;
        height: 40px;
        padding: 6px 12px;
        font-size: 15px;
        line-height: 1.42857143;
        color: #fff;
        box-sizing: border-box;
    }

    .form-control:focus {
        border-color: rgba(59, 137, 245, 0.5);
        outline: 0;
        -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 8px rgba(59, 137, 245, 0.6);
        box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 8px rgba(59, 137, 245, 0.6);
    }

    .form-input {
        background: transparent;
        border: 2px solid rgba(255, 255, 255, 0.3);
        box-shadow: inset 0 0 4px rgba(255, 255, 255, 0.2), 0 0 4px rgba(255, 255, 255, 0.2);
    }

    .save-div {
        color: #fff;
        font-weight: bold;
    }

    .save-checkbox {
        float: left;
    }

    .save-pass {
        float: left;
    }

    .signup {
        font-weight: 400;
        font-size: 13px;
        color: #fff;
        margin-top: 8px;
        display: block;
    }

    .signup-a {
        color: #468BEA;
        font-weight: bold;
    }
</style>
<div class="btnbg">
</div>

<div class="signin" style="background-color: antiquewhite">
    <center><h1 style="color: darkmagenta">重置密码</h1></center>
    <div class="box" style="background-color: darkorange">
        <form class="signin-form" method="post" action="/THMServer/resetPassword.php">
            <div class="" style="margin: 20px 0;">
                <input class="form-control form-input" placeholder="请输入新密码" type="password" name="password"/>
            </div>
            <div class="save-div">
                <div style="color: red;text-align: right;width:100%;padding-right: 9px" class=""></div>
            </div>
            <input type="submit" class="btn btn-primary btn-submit" value="提&nbsp;&nbsp;交">
        </form>
    </div>
</div>
<script>

</script>
</body>
</html>
